const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
import Dialog from '../../../dist/dialog/dialog';
var app = getApp();
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    domain:app.data.host,
    choose_name: "手机号",
    dialog_keyword: "",
    show: false,
    keyword: "",
    value_category: 2,
    show_tips: false,
    page: 1,
    total_page: 0,
    list: [],
    customer_archive_id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },
  search(){
    this.setData({show_tips:false});
    this.getData()
  },
  close() {
    this.setData({
      show: false,
      dialog_keyword:""
    });
  },
  change_handle() {
    this.setData({
      show_tips: !this.data.show_tips
    });
  },
  link(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/mircoApp/customer_file/info/info?id=' + id,
    })
  },
  goto() {
    wx.navigateTo({
      url: '/mircoApp/customer_file/fill_in/fill_in',
    })
  },
  choose(e) {
    let value_category = e.currentTarget.dataset.num;
    let choose_name = value_category == 2 ? '手机号' : value_category == 1 ? '客户名称' : value_category == 3 ? '项目' : '老师名称';
    this.setData({
      choose_name,
      value_category,
      show_tips: false
    });
  },
  handle(e) {
    let item = e.currentTarget.dataset.item;
    this.data.customer_archive_id = item.id;
    this.setData({
      show: true
    })
  },
  handle_del(e){
    let item = e.currentTarget.dataset.item;
    Dialog.confirm({
      title: '确定删除该客户档案？',
    })
      .then(() => {
        app._postNetWork({
          url: app.getNetAddresss("plugin.customer-archive.frontend.customer-archive.del-customer"),
          data: {customer_archive_id:item.id},
          success: resdata => {
            let {
              result,
              msg,
            } = resdata.data;
            app.tips(msg);
            if (result) {
                let index = this.data.list.findIndex(c_item=>c_item.id == item.id);
                this.data.list.splice(index,1);
                this.setData({list:this.data.list});
            }
          }
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  submit() {
    let json = {
      customer_archive_id: this.data.customer_archive_id,
      mobile: this.data.dialog_keyword
    };
    app._postNetWork({
      url: app.getNetAddresss("plugin.customer-archive.frontend.customer-archive.transfer-customer"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
        } = resdata.data;
        app.tips(msg);
        if (result) {
          this.getData();
          this.setData({
            show: false,
            dialog_keyword: ""
          });
        }
      }
    })


  },
  getData(page = 1) {
    let json = {
      page,
      keyword: this.data.keyword
    };
    app._postNetWork({
      url: app.getNetAddresss("plugin.customer-archive.frontend.customer-archive.get-list"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          let list = page == 1 ? data.list.data : this.data.list.concat(data.list.data);
          this.data.page = data.list.current_page;
          this.data.total_page = data.list.last_page;
          this.setData({
            list
          });
        } else {
          app.tips(msg);
        }
      }
    })

  },
  getMoreData() {
    if (this.data.page >= this.data.total_page) return;
    let page = this.data.page + 1;
    this.getData(page);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
    this.setData({show_tips:false});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getMoreData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})